if exists("b:did_ftplugin")
	finish
endif

function! s:markdown_mathjax() abort
    if !exists('g:loaded_mathjax_vim_plugin')
        try
            packadd mathjax-support-for-mkdp
        catch /.*/
            echoerr v:exception
        endtry
    endif
    if exists('g:loaded_mathjax_vim_plugin') && g:loaded_mathjax_vim_plugin
    endif
endfunction

function! s:markdown_preview() abort
    if !exists('g:loaded_mkdp')
        try
            packadd markdown_preview.vim
        catch /.*/
            echoerr v:exception
        endtry
    endif
    if exists('g:loaded_mkdp') && g:loaded_mkdp
        let g:mkdp_auto_start = 0
        let g:mkdp_auto_open = 0
        " for normal mode
        nmap <silent> <F8> <Plug>MarkdownPreview
        " for insert mode
        imap <silent> <F8> <Plug>MarkdownPreview
        " for normal mode
        nmap <silent> <F9> <Plug>StopMarkdownPreview
        " for insert mode
        imap <silent> <F9> <Plug>StopMarkdownPreview
        let g:preview_map = get(g:, "preview_map", { "name" : "+preview" })
        let g:preview_map.m = [ "<Plug>MarkdownPreview", "markdown" ]
        let g:preview_map.s = [ "<Plug>StopMarkdownPreview", "stop-mkdp" ]
        let g:space_keymap = get(g:, "space_keymap", { "name" : "+space" })
        let g:space_keymap.p = g:preview_map
    endif
endfunction

function! s:pandoc() abort
    if !exists('g:pandoc#loaded')
        try
            packadd vim-pandoc
            packadd vim-pandoc-syntax
            augroup VimPandoc
                au!
                call pandoc#command#Init()
            augroup END
        catch /.*/
            echoerr v:exception
        endtry 
    endif    
    if exists('g:pandoc#loaded') && g:pandoc#loaded
        let g:pandoc_keymap = get(g:, "pandoc_keymap", { "name" : "+async" })
        let g:pandoc_keymap.p = [ ":Pandoc", "pandoc" ]
    endif    
endfunction 

call s:markdown_preview()
call s:markdown_mathjax()

call s:pandoc()
